package com.wei.czz.common.vo.common;

import com.alibaba.excel.annotation.ExcelProperty;
import com.wei.czz.common.dto.common.NameValue;
import com.wei.czz.common.tool.BaseEntity;
import com.wei.czz.common.utils.ValidationUtils;
import lombok.Getter;
import lombok.Setter;

import javax.validation.constraints.NotNull;
import java.lang.reflect.Field;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2024-06-26 18:55:29
 * className: UploadVo 文件导入基础参数类
 * version: 1.0
 * description:
 */
@Getter
@Setter
public abstract class UploadVo extends BaseEntity {

    /**
     * 序号
     */
    @NotNull(message = "序号不能为空")
    @ExcelProperty(value = "序号", index = 0)
    protected Integer serial;

    /**
     * 错误提示
     */
    @ExcelProperty(value = "错误提示")
    private String errorMessage;

    /**
     * 获取导出文件标题列方法
     * @return 列名称列表
     */
    public List<List<String>> getTemplatePropertyList() {
        List<List<String>> list = new ArrayList<>();
        // 调用类class对象
        Class<? extends UploadVo> aClass = this.getClass();
        // 调用类父类class对象
        Class<?> superclass = aClass.getSuperclass();

        Field[] fields = superclass.getDeclaredFields();
        for (Field field : fields) {
            if ("errorMessage".equals(field.getName())) {
                continue;
            }
            ExcelProperty annotation = field.getAnnotation(ExcelProperty.class);
            int valueLength;
            if (Objects.isNull(annotation) || (valueLength = annotation.value().length) == 0) {
                continue;
            }
            list.add(Collections.singletonList(annotation.value()[valueLength - 1]));
        }
        fields = aClass.getDeclaredFields();
        for (Field field : fields) {
            ExcelProperty annotation = field.getAnnotation(ExcelProperty.class);
            int valueLength;
            if (Objects.isNull(annotation) || (valueLength = annotation.value().length) == 0) {
                continue;
            }
            list.add(Collections.singletonList(annotation.value()[valueLength - 1]));
        }
        return list;
    }

    /**
     * 获取模板字段名称列表
     * @return 字段名称列表
     */
    public List<NameValue<String>> getFailPropertyList() {
        List<NameValue<String>> list = new ArrayList<>();

        Class<? extends UploadVo> aClass = this.getClass();
        Field[] fields = aClass.getDeclaredFields();

        for (Field field : fields) {
            ExcelProperty annotation = field.getAnnotation(ExcelProperty.class);
            int valueLength;
            if (Objects.isNull(annotation) || (valueLength = annotation.value().length) == 0) {
                continue;
            }
            NameValue<String> nameValue = new NameValue<>();
            nameValue.setName(annotation.value()[valueLength - 1]);
            nameValue.setValue(field.getName());
            list.add(nameValue);
        }
        return list;
    }

    /**
     * 校验导入数据对象是否正确
     * @return 错误提示
     */
    public String valid() {
        return ValidationUtils.valid(this);
    }

}
